//
//  NYTeachersViewController.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/6.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYTeachersViewController.h"

//request
#import "NYTeacherRequest.h"

//view
#import "NYTeacherTableViewCell.h"
#import "NYTeacherSectionView.h"

@interface NYTeachersViewController ()<QMUITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *sectionMutaArray;
@end

@implementation NYTeachersViewController

#pragma mark - Life Cycle Methods
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadTheRequestMethod];
}
#pragma mark - Intial Methods
- (void)initTableView {
    [super initTableView];
    
//    self.tableView.estimatedRowHeight = 200;
}
- (void)configNimbus {
    [super configNimbus];
    
//    [self.tableView registerClass:[NYTeacherTableViewCell class] forCellReuseIdentifier:[NYTeacherTableViewCell className]];
    [self.tableView registerClass:[NYTeacherSectionView class] forHeaderFooterViewReuseIdentifier:[NYTeacherSectionView className]];
    self.tableViewModel.delegate = self;
}
- (void)loadTheRequestMethod {
    
    [self setLoading:YES animation:YES];
    [self configNimbus];
    
    @weakify(self);
    [[NYTeacherRequest requestWithParams:@{kRequestKeyChannel:@(NYUserManager.channelID)}] startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NSArray<NYTeacherModel *> * _Nonnull data) {
        
        @strongify(self);
        [self configNimbusDataForMutableSections:data];
    } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
        NSLogError(@"%@",error);
    }];
}
- (void)configNimbusDataForMutableSections:(NSArray<NYTeacherModel *> *)arrayM {
    
    NSEnumerator *arrayEnumerator = [arrayM objectEnumerator];
    NYTeacherModel *obj;
    while ((obj = [arrayEnumerator nextObject]) != nil) {
        
        [self.sectionMutaArray addObject:obj.cname];
        [self.tableViewModel addSectionWithTitle:nil];
        [self.tableViewModel addObjectsFromArray:[obj list]];
    }
    [self.tableView reloadData];
    [self setSuccess];
}
#pragma mark - Network Methods
- (void)refreshTheDataSource {
    
    [self.tableViewModel removeAll];
    
    [self loadTheRequestMethod];
}

#pragma mark - Target Methods

#pragma mark - Public Methods

#pragma mark - Private Method

#pragma mark - External Delegate

#pragma mark - QMUITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return __WGHeight(45);
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    NYTeacherSectionView *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:[NYTeacherSectionView className]];
    headView.title = self.sectionMutaArray[section];
    return headView;
}

#pragma mark - Setter Getter Methods
- (NSMutableArray *)sectionMutaArray {
    if (!_sectionMutaArray) {
        _sectionMutaArray = [NSMutableArray array];
    }
    return _sectionMutaArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
